package com.lennon.eoi.autoconfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import com.lennon.eoi.exception.EOIException;
import com.lennon.eoi.model.ExcelConfigVO;
import com.lennon.eoi.util.XmlUtils;

/**
 * 将配置文件初始化到
 * 
 * @author lennon
 * @time 2017年8月27日下午12:12:36
 *
 */
@Configuration
public class ExcelXmlAutoConfig {
    
	/** 日志记录对象 */
    private static Logger logger = LoggerFactory.getLogger(ExcelXmlAutoConfig.class);
	
    /** 配置项 */
	private static Map<String, Map<String,ExcelConfigVO>> execlConfigMap = null; 
	
	static {
	    initExeclXmlConfig();
	}
	
	/**
	 * 初始化配置项
	 */
	public static void initExeclXmlConfig() {
	    
		SAXReader reader = new SAXReader();
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("ExcelConfig.xml");
		
		try {
		    Document doc = reader.read(inputStream);
			execlConfigMap = XmlUtils.Dom2MapWithAttrs(doc);
		} catch (DocumentException e) {
			logger.error("读取配置文件ExcelConfig.xml出错! 异常信息为：" + e);
		} finally {
			if(inputStream != null) {
			    try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error("读取配置文件ExcelConfig.xml出错! 异常信息为：" + e);
                }
			}
		}
	}
	
	/**
	 * 返回一个模板id为key的EXCLE模板配置
	 * 
	 * @param key
	 * @return
	 */
	public static Map<String, ExcelConfigVO> getExcelConfigMap(String key)
	{
	    if(execlConfigMap == null) {
	        throw new EOIException("模板初始化失败");
	    }
		return execlConfigMap.get(key);
	}
}
